import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {ElMessage} from "element-plus";

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect: '/scene',
    children: [
      {path: '/home', component: () => import('../views/HomeView.vue')},
      {path: '/userInfo', component: () => import('../views/user/UserInfo.vue')},
      {path: '/community', component: () => import('@/views/community/CommunityView.vue')},
      {path: '/guide', component: () => import('../views/guide/GuideView.vue')},
      {path: '/inform', component: () => import('../views/inform/InformView.vue')},
      {path: '/informDetail', component: () => import('../views/inform/InformDetail.vue')},
      {path: '/scene', component: () => import('@/views/scene/SceneView.vue')},
      {path: '/like', component: () => import('@/views/like/LikeView.vue')},
      {path: '/xqy', component: () => import('../views/scene/xqy.vue')},
      {path: '/additionArea', component: () => import('../views/scene/additionArea.vue')},
      {path: '/Strategy', component: () => import('../views/scene/Strategy.vue')},
      {path: '/Strategy2', component: () => import('../views/scene/Strategy2.vue')},
      {path: '/Strategy3', component: () => import('../views/scene/Strategy3.vue')},
      {path: '/Strategy4', component: () => import('../views/scene/Strategy4.vue')},
    ]
  },
  {path: '/login', component: () => import('../views/LoginView')},
  {path: '/addGuide', component: () => import('../views/guide/AddGuide.vue')},
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

/**
 * 在路由切换之前判断用户是否已登录。
 * 如果用户未登录(localStorage里没登录的用户数据)且试图访问需要登录
 * @param {Object} to - 将要进入的目标路由对象
 * @param {Object} from - 当前的路由对象
 * @param {Function} next - 前进到下一个路由的函数
 */
router.beforeEach((to, from, next) => {
  // 从本地存储中获取用户信息
  let user = localStorage.user;
  // 判断如果用户未登录且试图访问的页面不是登录页，则重定向到登录页，并提示需先登录
  if (to.path !== '/login' && !user && to.path !== '/') {
    next({ path: '/login', query: { redirect: to.fullPath } },ElMessage.error('请先登录'));
  } else {
    // 允许路由切换
    next();
  }
});

export default router
